package com.orcamento.dao;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.orcamento.entity.Account;
import com.orcamento.security.Encrypter;

public class AccountDao extends AbstractDao<Account> {

	public AccountDao() {
		super(Account.class);
	}

	public Account findByLoginAndPassword(String login, String password) {
		Criteria criteria = session().createCriteria(Account.class);
		criteria.add( Restrictions.eq("login", login) );
		criteria.add( Restrictions.eq("password", Encrypter.password(password)) );
		criteria.add( Restrictions.eq("status", Boolean.TRUE) );
		return (Account) criteria.uniqueResult();
	}

	public Account findById(Long primaryKey) {
		return super.findById(primaryKey);
	}

	public boolean hasLogin(String login) {
		Criteria criteria = session().createCriteria( Account.class ).add( Restrictions.eq("login", login) );
		return criteria.uniqueResult() != null;
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public Account save(Account account) {
		return saveEntity(account);
	}

}
